package listtest;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-06-29
 * Time: 11:43
 */
public class MyArrayList implements IList {

    public int[] array;
    public int usedSize;//数组有效长度
    public final static int DEFAULT_CAPACITY = 10;//数组默认长度


    public MyArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    //判满
    @Override
    public boolean isFull() {
        if (this.usedSize == this.array.length) {
            return true;
        }
        return false;
    }

    //扩容
    private void grow() {
        this.array = Arrays.copyOf(this.array, this.array.length * 2);
    }

    @Override
    public void add(int data) {
        if (isFull()){
            //扩容
            grow();
        }
        this.array[this.usedSize++] = data;

    }

    @Override
    public void add(int pos, int data) {
        if (isFull()){
            //扩容
            grow();
        }

        /**注意以下：
         * 要在某位置插入，这个位置的，前驱一定不能为空
         */

        //从后往前移动元素
        for (int i = this.usedSize-1; i > 0; i--) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;

    }

    //元素是否被包含
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (this.array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //顺序表判空
    private boolean isEmpty() {
        return this.usedSize == 0;
    }
    private void checkOfList() {
        if (isEmpty()) {
           throw  new EmptyException("顺序表为空");
        }
    }

    //检查下标是否合法
    private void checkOfPos(int pos) {
        if (pos < 0 || pos >= this.usedSize) {
           throw  new PosException("下标不合法");
        }
    }

    @Override
    public int get(int pos) {
        try {
            checkOfList();//检查顺序表是否为空
            checkOfPos(pos);//检查顺序表下标否合法

            return this.array[pos];
        }catch (EmptyException e) {
            e.printStackTrace();
        }catch (PosException e) {
            e.printStackTrace();

        }

        return -1;
    }

    @Override
    public void set(int pos, int value) {

        try {
            checkOfList();//检查顺序表是否为空
            checkOfPos(pos);//检查顺序表下标否合法

            this.array[pos] = value;

        }catch (EmptyException e) {
            e.printStackTrace();
        }catch (PosException e) {
            e.printStackTrace();

        }



    }

    @Override
    public void remove(int toRemove) {
        try {
            checkOfList();//判空
            int pos = indexOf(toRemove);//返回要删除数据的下标

            if (pos == -1) {
                System.out.println("找不到你要删除的元素");
            }

            for (int i = pos; i < usedSize-1; i++) {
                this.array[i] = array[i+1];/*这里注意，[i+1]位置也不能为空*/
            }
            this.usedSize--;

        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {

        return this.usedSize;
    }

    @Override
    public void clear() {
        //如果是自定义的，引用类型，要把引用置为空
        /*for (int i = 0; i < usedSize; i++) {
            this.array[i] = null;
        }
        this.usedSize = 0;*/


        //基本类型，直接来即可
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
    }
}
